package com.shengyin.ec.controller.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import com.shengyin.ec.model.entity.Company;
import com.shengyin.ec.model.entity.Personal;
import com.shengyin.ec.service.account.BookInfoService;
import com.shengyin.ec.service.account.FavoritenService;
import com.shengyin.ec.service.account.PersonalService;
import com.shengyin.ec.service.account.ProvinceService;
import com.shengyin.ec.service.account.ReceiverService;
import com.shengyin.ec.service.alliance.AllianceService;
import com.shengyin.ec.service.alliance.AllianceViewInfoService;
import com.shengyin.ec.service.cluster.ClusterService;
import com.shengyin.ec.service.companyorder.CompanyOrderService;
import com.shengyin.ec.service.goods.GoodsService;
import com.shengyin.ec.service.order.OrderService;
import com.shengyin.ec.service.payment.PaymentService;

public class LoginFilter implements Filter {

	@Autowired
	PersonalService personalService;
	
	@Autowired
	ProvinceService provinceService;
	
	@Autowired
	ReceiverService receiverService;
	
	@Autowired
	OrderService orderService;
	
	@Autowired
	FavoritenService favoritenService;
	
	@Autowired
	GoodsService goodsService;
	
	@Autowired
	PaymentService PaymentService;
	
	@Autowired
	Md5PasswordEncoder passwordEncoder;

	@Autowired
	AllianceService allianceService;
	
	@Autowired
	ClusterService clusterService;
	@Autowired 
	CompanyOrderService companyOrderService;
	
	@Autowired 
	BookInfoService bookInfoService;
	
	//add by lcl 2012-10-10
	@Autowired
	AllianceViewInfoService allianceViewInfoService;
	
	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request=(HttpServletRequest)arg0;
		HttpServletResponse response=(HttpServletResponse)arg1;
		
		HttpSession session =request.getSession();
		
		Object user= session.getAttribute("user");
		
		Object alliance= session.getAttribute("alliance");
		Object cluster= session.getAttribute("cluster");
		
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		String uri=request.getRequestURI();
		
		
		if(user==null&&alliance==null&&cluster==null){
			response.sendRedirect(basePath+"view/login.jsp");
		}
		else{
			String username=request.getParameter("username");
			
			Md5PasswordEncoder passwordEncoder = new Md5PasswordEncoder();	
			
			String password=passwordEncoder.encodePassword("shengyin",request.getParameter("password"));
			
			
			chain.doFilter(request, response);
			
			
			
		}
		
		
		

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}
